package io.renren.modules.sp.controller.front;

import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sp.dto.*;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpMatchItemEntity;
import io.renren.modules.sp.entity.SpTeamEntity;
import io.renren.modules.sp.service.*;
import io.renren.modules.sys.dao.SysCommDao;
import io.renren.modules.sys.dao.SysParamsDao;
import io.renren.modules.sys.service.SysParamsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-04-20
*/
@RestController
@RequestMapping("front/spmatch")
@Api(tags="领队成员")
public class FrontMatchController extends FrontBaseController {

    @Autowired
    private SysCommDao commDao;
    @Autowired
    private SpMatchItemService spMatchItemService;
    @Autowired
    private SpMatchService spMatchService;
    @Autowired
    private SysParamsService sysParamsService;
    @Autowired
    private SpTeamService spTeamService;
    @Autowired
    private SpExcludeAreaService spExcludeAreaService;

    @GetMapping("{id}")
    public Result<List<SpMatchItemDTO>> update(@PathVariable("id") Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("matchId", id);
        List<SpMatchItemDTO> list = spMatchItemService.list(params);
        return new Result<List<SpMatchItemDTO>>().ok(list);
    }

    @GetMapping("matchItem/{id}")
    public Result<SpMatchItemEntity> matchItem(@PathVariable("id") Long id, HttpServletRequest request) {
        SpMatchItemEntity entity = spMatchItemService.selectById(id);
        return new Result<SpMatchItemEntity>().ok(entity);
    }


    @GetMapping("info/{id}")
    public Result<SpMatchDTO> info(@PathVariable("id") Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("matchId", id);
        SpMatchEntity matchEntity = spMatchService.selectById(id);
        //entity 转 dto
        SpMatchDTO spMatchDTO = ConvertUtils.sourceToTarget(matchEntity, SpMatchDTO.class);
        //把beginDate和endDate转成字符串
        String matchDate = DateUtils.format(matchEntity.getBeginDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        matchDate += "~" + DateUtils.format(matchEntity.getEndDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        spMatchDTO.setMatchDate(matchDate);
        //组合报名时间
        String signupDate = DateUtils.format(matchEntity.getBeginSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        signupDate += "~" + DateUtils.format(matchEntity.getEndSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        spMatchDTO.setSignupDate(signupDate);
        return new Result<SpMatchDTO>().ok(spMatchDTO);
    }

    // 查询全局配置
    @GetMapping("sysParams")
    public Result<Map<String, Object>> sysParams() {
        Map<String, Object> params = new HashMap<>();
        params = sysParamsService.getSysParams();
        return new Result<Map<String, Object>>().ok(params);
    }

    /**
     * 获取报名赛项条目
     * @param matchId
     * @param matchItemId
     * @param city
     * @return
     */
    @GetMapping("matchItem")
    public Result<Map<String, Object>> matchItem(@RequestParam Long matchId, Long matchItemId, Long teamId, String city) {
        Map<String, Object> result = new HashMap<>();

        SpMatchEntity matchEntity = spMatchService.selectById(matchId);

        //07月02日~07月12日
        String beginSignup = DateUtils.format(matchEntity.getBeginSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        String endSignup = DateUtils.format(matchEntity.getEndSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);

        String beginDate = DateUtils.format(matchEntity.getBeginDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        String endDate = DateUtils.format(matchEntity.getEndDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);


        result.put("begindate", matchEntity.getBeginDate());
        result.put("signdate", beginSignup + "~" + endSignup);
        result.put("match_name", matchEntity.getMatchName());
        result.put("matchdate", beginDate + "~" + endDate);
        result.put("promiss_file", matchEntity.getPromissFile());
        result.put("needPromissFile", matchEntity.getNeedPromissFile());


        //判断是否可以报名
        boolean canSignFlag = spMatchService.isCanSignUp(matchId, matchItemId, teamId, city);

        List<Map<String, Object>> list = spMatchService.queryMatchItem(matchId, matchItemId, city);
        result.put("canSignFlag", canSignFlag);
        result.put("detail", list);
        return new Result<Map<String, Object>>().ok(result);
    }

}
